package com.tsn.controller;


import com.alibaba.fastjson.JSONObject;
import com.tsn.common.Result;
import com.tsn.entity.Personage;
import com.tsn.entity.Site;
import com.tsn.service.PersonageService;
import com.tsn.service.SiteService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 名人 前端控制器
 * </p>
 *
 * @author tsn
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/tsn/personage")
public class PersonageController {
    @Autowired
    private PersonageService personageService;
//    @RequiresAuthentication
    @GetMapping("/getPersonages")
    public Result getPersonageList(){
        return  Result.success(personageService.list());
    }

    @RequiresAuthentication
    @PostMapping("/addPersonage")
    public Result addPersonage(@RequestBody JSONObject article){
//        System.out.println(site);
        if(article==null)
            return Result.failure("数据异常，请重试");
        Personage personage = new Personage();
        personage.setPersonageName(article.getString("title"));
        personage.setPersonageEditor(article.getString("editor"));
        personage.setPersonageDeed(article.getString("content"));
        personageService.save(personage);
        return Result.success(null);
    }
    @GetMapping("/getAttentionPersonages")
    public  Result getAttentionPersonages(@RequestParam("userId")Integer userId){
        return  personageService.listAttentionPersonages(userId);
    }

    @GetMapping("/getPersonageOne")
    public  Result getSiteById(@RequestParam("id")Integer id){
        return  Result.success(personageService.getById(id));
    }
    @PutMapping("/addCover")
    public Result addCover(@RequestBody JSONObject object){
        String cover = object.getString("cover");
        Integer id = object.getInteger("id");
        Personage personage = personageService.getById(id);
        personage.setCover(cover);
        personageService.updateById(personage);
        return Result.success(null);
    }
}

